package com.lemon.smartsafetynew.ui.fragment.patrol;

import android.os.Bundle;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.app.RefreshType;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.ui.fragment.base.BasePullToRefreshFragment;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafety.view.list.MultiStateView;
import com.lemon.smartsafetynew.entity.User;
import com.lemon.smartsafetynew.entity.event.common.TypeEvent;
import com.lemon.smartsafetynew.entity.patrol.SafePatrolTaskVo;
import com.lemon.smartsafetynew.net.services.SafePatrolServices;
import com.lemon.smartsafetynew.ui.activity.patrol.SafePatrolTaskDetailActivity;
import com.lemon.smartsafetynew.ui.adapter.patrol.PatrolTaskListAdapter;
import com.lemon.smartsafetynew.util.common.AppSharePreferenceUtil;
import com.lemon.smartsafetynew.util.constants.KeyConstants;
import com.lemon.smartsafetynew.util.constants.SafeCloudConstants;
import com.zhy.http.okhttp.OkHttpUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import kale.adapter.LemonRcvAdapter;

/**
 * 任务路线列表
 * Created by Andrew on 2017/7/28.
 */

public class SafePatrolTaskListFragment extends BasePullToRefreshFragment<SafePatrolTaskVo, List<SafePatrolTaskVo>>
        implements RecyclerTClickListener<SafePatrolTaskVo> {

    private static final String TAG = "SafePatrolTask";

    /**
     * 任务状态。为空则查询所有
     * ("1", "待执行"),
     * ("2", "执行中"),
     * ("3", "已完成"),
     * ("4", "已超时");
     */
    private String status = "";
    private User user = null;

    @Override
    protected void getBaseArguments() {
        super.getBaseArguments();

        Bundle bundle = getArguments();

        if (bundle != null) {
            status = bundle.getString(KeyConstants.KEY_SAFE_PATROL_TASK_TYPE);
        }
    }

    public static SafePatrolTaskListFragment newInstance(String type) {

        Bundle args = new Bundle();
        args.putString(KeyConstants.KEY_SAFE_PATROL_TASK_TYPE, type);

        SafePatrolTaskListFragment fragment = new SafePatrolTaskListFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onPullRefresh() {

        //clear data
//        mDataList.clear();
//        mAdapter.notifyDataSetChanged();

        LemonRequestParams params = new LemonRequestParams("");
        params.put("f_state", status);
        params.put("date", AppSharePreferenceUtil.getInstance().getString(SafeCloudConstants.SAFE_PATROL_CHOOSE_DATE, ""));
        params.put("pageNum", currentPage);
        params.put("pageSize", pageSize);

        SafePatrolServices
                .init()
                .getSafePatrolTaskList(RefreshType.REFRESH, params, status);

    }

    @Override
    public void onLoadMore() {

        LemonRequestParams params = new LemonRequestParams("");
        params.put("f_state", status);
        params.put("date", AppSharePreferenceUtil.getInstance().getString(SafeCloudConstants.SAFE_PATROL_CHOOSE_DATE, ""));
        params.put("pageNum", currentPage);
        params.put("pageSize", pageSize);

        SafePatrolServices
                .init()
                .getSafePatrolTaskList(RefreshType.LOAD_MORE, params, status);

    }

    @Override
    public boolean goRefreshOnCreate() {
        return true;
    }

    @Override
    public LemonRcvAdapter<SafePatrolTaskVo> getLemonAdapter() {
        return new PatrolTaskListAdapter(mDataList, this);
    }

    @Override
    public String getHttpTag() {
        return KeyConstants.URL_GET_SAFE_PATROL_TASK_LIST + status;
    }

    @Override
    protected void initViews() {
        super.initViews();
    }

    /**
     * 下拉刷新
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(TypeEvent event) {
        mMultiStateView.setViewState(MultiStateView.VIEW_STATE_CONTENT);
        mPullToRefreshRecyclerView.beginRefreshing();

    }

    @Override
    public void onItemClick(SafePatrolTaskVo data, int position) {

        Bundle bundle = new Bundle();
        bundle.putSerializable(KeyConstants.KEY_SAFE_PATROL_TASK, data);
        UIHelper.showActivity(getActivity(), bundle, SafePatrolTaskDetailActivity.class);
    }

//    @Override
//    public void onResume() {
//        super.onResume();
//        Log.e(TAG, "【onResume】 status = " + status);
//    }
//
//    @Override
//    public void onPause() {
//        super.onPause();
//        Log.e(TAG, "【onPause】 status = " + status);
//    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        //Log.e(TAG, "【onDestroyView】 status = " + status);
        OkHttpUtils.getInstance().cancelTag(KeyConstants.URL_GET_SAFE_PATROL_TASK_LIST + status);
    }
}
